package com.hucheng.rest.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.hucheng.common.exception.EduException;
import com.hucheng.common.page.CommonPager;
import com.hucheng.common.page.PageParameter;
import com.hucheng.common.response.ServiceCoreVO;
import com.hucheng.common.utils.RedisUtils;
import com.hucheng.common.utils.ValidatorUtils;
import com.hucheng.rest.base.BaseAction;
import com.hucheng.seckill.query.SeckillGoodsQuery;
import com.hucheng.seckill.service.SeckillGoodsService;
import com.hucheng.seckill.vo.SeckillGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-06-25
 * @Description: 商品信息API
 **/
@Controller("seckillGoodsAction")
public class SeckillGoodsAction extends BaseAction {

    @Reference
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 秒杀商品列表
     *
     * @return
     * @throws EduException
     */
    public ServiceCoreVO goodsListV100() throws EduException {

        String currentPage = getRequestContentValue("currentPage");
        String pageSize = getRequestContentValue("pageSize");

        PageParameter pageParameter = new PageParameter();
        if (currentPage != null && !currentPage.equals("null") &&
                pageSize != null && !pageSize.equals("null")) {
            pageParameter = new PageParameter(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        }

        SeckillGoodsQuery seckillGoodsQuery = new SeckillGoodsQuery();
        seckillGoodsQuery.setPageParameter(pageParameter);
        CommonPager<SeckillGoodsVO> commonPager = seckillGoodsService.listByPage(seckillGoodsQuery);

        return response(commonPager);
    }

    /**
     * 秒杀商品详情页
     *
     * @return
     */
    public ServiceCoreVO goodsDetailV100() throws EduException {
        String goodsId = getRequestContentValue("goodsId");

        if (ValidatorUtils.isEmpty(goodsId)) {
            throw new EduException(120000);
        }

        SeckillGoodsVO seckillGoodsVO = seckillGoodsService.getSeckillGoodsByGoodsId(Integer.parseInt(goodsId));

        return response(seckillGoodsVO);
    }

    /**
     * 秒杀商品
     *
     * @return
     */
    public ServiceCoreVO seckillGoodsV100() throws EduException {

        String goodsId = getRequestContentValue("goodsId");

        if (ValidatorUtils.isEmpty(goodsId)) {
            throw new EduException(120000);
        }

        if (ValidatorUtils.isEmpty(userId + "")) {
            throw new EduException(100007);
        }

        String token = null;
        try {
            token = RedisUtils.tryLock(redisTemplate, userId + "", 5000);
            if (token != null) {
                // 执行业务代码
                seckillGoodsService.seckillGoods(userId, Integer.parseInt(goodsId));
            }
        } finally {
            if (token != null) {
                RedisUtils.unlock(redisTemplate, userId + "", token);
            }
        }

        return response("秒杀排队中，请稍等~~");
    }

    /**
     * 秒杀结果
     *
     * @return
     * @throws EduException
     */
    public ServiceCoreVO seckillResultV100() throws EduException {

        String goodsId = getRequestContentValue("goodsId");

        if (ValidatorUtils.isEmpty(goodsId)) {
            throw new EduException(120000);
        }

        if (ValidatorUtils.isEmpty(userId + "")) {
            throw new EduException(100007);
        }

        Integer seckillOrderId = seckillGoodsService.seckillResult(userId, Integer.parseInt(goodsId));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("seckill_order_id", seckillOrderId);
        return response(jsonObject);
    }


    //秒杀去支付,支付失败或支付超时是否恢复秒杀库存,继续去秒杀


}
